#!/usr/bin/python3
# -*- coding:utf-8 -*-
# __author__ == taoyulong2018@gmail.com
# __time__ == 2023/8/29 17:10
# ===========================================
#       题目名称： 50. Pow(x, n)
#       题目地址： https://leetcode.cn/problems/powx-n/
#       题目描述： https://note.youdao.com/s/RRXNSXjR
# ===========================================


class Solution:
    def myPow(self, x: float, n: int) -> float:
        res, unsigned_n = 1.0, -n if n < 0 else n  # 先取无符号的N
        while unsigned_n != 0:
            """
              x = 2     unsigned_n = 10
              10 % 2    unsigned_n = 5      x = 4       res = 1
              5 % 2     unsigned_n = 2      x = 16      res = 4
              2 % 2     unsigned_n = 1      x = 256（16 * 16） res = 4
              1 % 2     unsigned_n = 0      x = 256*256      res = 4 * 256                        
            """
            if unsigned_n % 2 != 0:  # 如果是奇数
                res *= x
            x *= x
            unsigned_n //= 2

        return 1 / res if n < 0 else res


if __name__ == '__main__':
    s = Solution()
    # 1024.00000
    print(s.myPow(x=2.00000, n=10))
    # 9.26100
    print(s.myPow(x=2.10000, n=3))
    # 0.25000
    print(s.myPow(x=2.00000, n=-2))
